﻿using Microsoft.WindowsAPICodePack.Shell;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace VideoLib.Utils
{
    public static class ThumbHelper
    {
        public static async Task<BitmapImage> GetThumbnailByPath(string filePath)
        {
            ShellFile shellFile = ShellFile.FromFilePath(filePath);
            try
            {
                Bitmap? thumbnail = null;
                await Task.Run(() =>
                {
                    thumbnail = shellFile.Thumbnail.Bitmap;

                });

                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();

                MemoryStream memoryStream = new MemoryStream();
                thumbnail!.Save(memoryStream, ImageFormat.Png);
                memoryStream.Seek(0, SeekOrigin.Begin);
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.EndInit();

                return bitmapImage;
            }
            catch (Exception)
            {
                return new BitmapImage();
            }

        }
    }
}
